package com.kzd.btgame.main.popup

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import android.widget.*
import com.blankj.utilcode.util.TimeUtils
import com.kzd.btgame.R
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.entity.WelfareCoupon
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AlphaConfig
import razerdp.util.animation.AnimationHelper
import java.lang.Exception
import java.lang.StringBuilder


class CouponUsePopup(context: Context) : BasePopupWindow(context) {

    init {
        popupGravity = Gravity.CENTER
        setOverlayNavigationBar(false)
    }

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_coupon_use)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.findViewById<TextView>(R.id.tvCouponUseClose).setOnClickAntiShakeListener {
            dismiss()
        }
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withAlpha(AlphaConfig.IN)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withAlpha(AlphaConfig.OUT)
            .toDismiss()
    }

    fun fillData(welfareCoupon: WelfareCoupon): CouponUsePopup {
        contentView.findViewById<TextView>(R.id.tvCouponUseContent)
            .text = StringBuilder().append("可用范围：")
            .append("仅限《").append(welfareCoupon.game_name).append("》充值使用（限制游戏除外）\n有效期：")
            .append(
                TimeUtils.millis2String(welfareCoupon.start_time.toLong() * 1000, "yyyy-MM-dd")
            ).append("至").append(
                TimeUtils.millis2String(welfareCoupon.end_time.toLong() * 1000, "yyyy-MM-dd")
            ).append("\n使用条件：").append(
                try {
                    if (welfareCoupon.money.toInt() == 0)
                        "任意金额" else "满${welfareCoupon.money}元可用"
                } catch (e: Exception) {
                    "任意金额"
                }
            ).append("\n使用方法：\n1.使用App账号登录游戏;\n2.在游戏内购买道具;\n3.在支付页面选择可用的优惠券，完成支付。")

        return this
    }

}